$(function () {
    $("#jqGrid").jqGrid({
        datatype:'local',
        colModel: [
            { label: '分润id', name: 'benefitId', index: "transactionId", width: 100, key: true,align:'center' , sortable: false },
            { label: '姓名', name: 'name', width: 70,align:'center' , sortable: false },
            { label: '手机号码', name: 'phone', width: 80,align:'center'},
            { label: '身份', name: 'type', width: 45,align:'center',formatter:function (value,options,row) {
                    return value==3?"盟主":"普通";
                } , sortable: false},
            { label: '总分润', name: 'allAmount', width: 50,align:'center' , sortable: false,formatter:function (value,options,row) {
                    return value.toFixed(2);
                }},
            { label: '个人分润', name: 'selfAmount', width: 50,align:'center' , sortable: false,formatter:function (value,options,row) {
                    return value.toFixed(2);
                }},
            { label: '团队分润', name: 'teamAmount', width: 100,align:'center' , sortable: false,formatter:function (value,options,row) {
                    return value.toFixed(2);
                }},
            { label: '分润时间', name: 'benefitDate', width:75,align:'center' , sortable: false},
            { label: '总交易', name: 'allTran', width: 95,align:'center' , sortable: false,formatter:function (value,options,row) {
                    return value.toFixed(2);
                }},
            { label: '个人交易', name: 'selfTran', width: 95,align:'center' , sortable: false,formatter:function (value,options,row) {
                    return value.toFixed(2);
                }},
            { label: '团队交易', name: 'teamTran', width: 95,align:'center' , sortable: false,formatter:function (value,options,row) {
                    return value.toFixed(2);
                }},
            { label: '当月vip', name: 'thisGrade', width: 45,align:'center' , sortable: false},
            { label: '上月vip', name: 'upperGrade', width: 45,align:'center' , sortable: false},
            /*{ label: '操作',name:'agentId',formatter:function(value, options, row){
                    return   '<span class = "btn btn-success btn-sm" onclick = ""> 新增 </span>&nbsp;&nbsp;'+
                        '<span class = "btn btn-success btn-sm" onclick = ""> 修改 </span>&nbsp;&nbsp;';
                },align:'center'}*/
        ],
		viewrecords: true,
        height: 420,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 50,
        autowidth:true,
        subGrid:true,
        subGridRowExpanded:function (subgrid_id,row_id) {
            bindSubGrid(subgrid_id, row_id)
        },
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount",
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    }).setGridParam();


});


var vm = new Vue({
    el:'#rrapp',
    data:{
        q:{
            startDate:null,
            endDate:null,
            name:null,
            agentId:null,
        },
        showList: true,
        title:null,
        agentList:[],

    },
    methods: {
        //根据姓名查询合伙人
        query: function () {
            if(vm.q.name=='' || vm.q.name==null){
                vm.reload();
                return;
            }
            $.ajax({
                url:baseURL+"sys/sysAgent/byName",
                data:{"name":vm.q.name},
                dataType:"json",
                type:"post",
                success:function (r) {
                    if(r.list.length==1){
                        vm.q.agentId=r.list[0].agentId;
                        vm.reload();
                    }else if (r.list.length>1) {
                        openDialog(r);
                    }else if(r.list.length<1){
                        layer.msg("查无此人,请核实姓名")
                    }
                }

            })



        },
        searchOne:function(obj){
            var aEl=$(obj.srcElement);
            vm.q.agentId=$(aEl).parent().find("input").val();
            layer.close(agentDialog);
            vm.reload();
        },
        clean:function(){
            vm.showList = true;
            vm.q={
                startDate:null,
                endDate:null,
                name:null,
                agentId:null,
            };
            $("#endDate").val("");
            $("#startDate").val("");
            $("#jqGrid").jqGrid('clearGridData')
            $("#allAmount").text('0.00');
            $("#selfAmount").text('0.00');
            $("#teamAmount").text('0.00');
        },
        reload: function () {
            vm.showList = true;
            vm.q.startDate=$("#startDate").val();
            vm.q.endDate=$("#endDate").val();
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{'startDate': vm.q.startDate,'endDate':vm.q.endDate,'agentId':vm.q.agentId},
                datatype:'json',
                url: baseURL + 'sys/benefit/list',
                page:1
            }).trigger("reloadGrid");
            vm.countTotal();
        },
        downLoad:function () {
            window.location.href=baseURL+"sys/benefit/downLoad";
        },
        countTotal(){
            $.ajax({
                url:baseURL+"sys/benefit/countTotal",
                data:JSON.stringify(vm.q),
                contentType:"application/json",
                type:"post",
                success:function (r) {
                    if(r.code==0){
                        $("#allAmount").text(r.allAmount.toFixed(2));
                        $("#selfAmount").text(r.selfAmount.toFixed(2));
                        $("#teamAmount").text(r.teamAmount.toFixed(2));
                    }else {

                        $("#allAmount").text('0.00');
                        $("#selfAmount").text('0.00');
                        $("#teamAmount").text('0.00');
                        layer.msg("统计有误");
                    }

                }
            })
        }

    }
});
var agentDialog;
//如果查询到多个合伙人，打开对话框
function openDialog(r){
    for (var i = 0; i < r.list.length; i++) {
        vm.$set(vm.agentList, i, r.list[i]);
    }
    agentDialog=layer.open({
        type:1,
        title:"请选择",
        content:$("#agentList"),
        area:['400px','280px'],
        btn:['确定'],
        btn1:function (index) {
            layer.close(index);
        }
    })
}
//格式化时间
Date.prototype.Format = function (fmt) {
    var o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "h+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
};
//加载子表格数据
function bindSubGrid(subgrid_id, row_id) {
     // (2)子表格容器的id和需要展开子表格的行id，将传入此事件函数
        var subgrid_table_id;
        subgrid_table_id = subgrid_id + "_t";   // (3)根据subgrid_id定义对应的子表格的table的id

        var subgrid_pager_id;
        subgrid_pager_id = subgrid_id + "_pgr";  // (4)根据subgrid_id定义对应的子表格的pager的id
        var currRow=$("#jqGrid").jqGrid('getRowData',row_id);
        // (5)动态添加子报表的table和pager
        $("#" + subgrid_id).html("<table id='"+subgrid_table_id+"' class='scroll'></table><div id='"+subgrid_pager_id+"' class='scroll'></div>");

        // (6)创建jqGrid对象
        $("#" + subgrid_table_id).jqGrid({
            url: baseURL + 'sys/benefit/detail',  // (7)子表格数据对应的url，注意传入的contact.id参数
            datatype: "json",
            postData:{'benefitId':currRow.benefitId,'thisGrade':currRow.thisGrade},
            colModel: [
                { label: '终端名称', name: 'terminalName', width: 45,align:'center' , sortable: false},
                { label: '是否是vip', name: 'isVip', width: 45,align:'center' , sortable: false},
                { label: '总交易', name: 'allInfoTran', width: 45,align:'center' , sortable: false,formatter:function (value,options,row) {
                        return value.toFixed(2);
                    }},
                { label: '个人交易', name: 'selfInfoTran', width: 45,align:'center' , sortable: false,formatter:function (value,options,row) {
                        return value.toFixed(2);
                    }},
                { label: '团队交易', name: 'teamInfoTran', width: 45,align:'center' , sortable: false,formatter:function (value,options,row) {
                        return value.toFixed(2);
                    }},
                { label: '总分润', name: 'allInfoAmount',width: 100,align:'center' , sortable: false ,formatter:function (value,options,row) {
                        return value.toFixed(2);
                    }},
                { label: '个人分润', name: 'selfInfoAmount', width: 70,align:'center' , sortable: false,formatter:function (value,options,row) {
                        return value.toFixed(2);
                    } },
                { label: '团队分润', name: 'teamInfoAmount', width: 80,align:'center' , sortable: false,formatter:function (value,options,row) {
                        return value.toFixed(2);
                    }},

            ],
            jsonReader: {   // (8)针对子表格的jsonReader设置
                root: "page.list",
                page: "page.currPage",
                total: "page.totalPage",
                records: "page.totalCount",
            },
            prmNames: {
                search: "search",
                page:"page",
                rows:"limit",
                order: "order"
            },
            pager: subgrid_pager_id,
            viewrecords: true,
            height: "100%",
            rowNum: 5
        }).setGridParam();

}
